package com.hnly.djdl.home

import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hnly.djdl.R
import com.hnly.djdl.components.ArrowTitleText
import com.hnly.djdl.model.Snack
import com.hnly.djdl.model.ic_sec_1
import com.hnly.djdl.model.ic_sec_2
import com.hnly.djdl.model.ic_sec_3
import com.hnly.djdl.model.ic_sec_4
import com.hnly.djdl.model.ic_sec_5
import com.hnly.djdl.model.ic_sec_6
import com.hnly.djdl.model.storyLists
import com.hnly.djdl.components.SnackImage
import com.hnly.djdl.model.P3
import com.hnly.djdl.theme.MainCardColor
import com.hnly.djdl.theme.MainThemeColor

@Composable
fun StoryListPage(onBackClick: () -> Unit = {}) {
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        item {
            ArrowTitleText(
                text = stringResource(id = R.string.app_name),
                modifier = Modifier,
                onArrowClick = onBackClick
            )
        }
        items(storyLists.size) { index ->
            val story = storyLists[index]
            DogItem(
                index = index,
                dog = story,
                modifier = Modifier
            )
        }
    }
}

@Composable
fun DogItem(
    index: Int,
    dog: Snack,
    modifier: Modifier = Modifier,
) {
    var expanded by remember { mutableStateOf(false) }
    Card(
        modifier = modifier
            .padding(top = 12.dp, start = 12.dp, end = 12.dp)
            .fillMaxWidth(),
        backgroundColor = MainCardColor
    ) {
        Column(
            modifier = Modifier
                .animateContentSize(
                    animationSpec = spring(
                        dampingRatio = Spring.DampingRatioNoBouncy,
                        stiffness = Spring.StiffnessMedium
                    )
                )
        ) {
            Row(modifier = Modifier.fillMaxWidth()) {
                DogIcon(
                    when (index % 10) {
                        1 -> P3.MENU_1.imageUrl
                        2 -> P3.MENU_2.imageUrl
                        3 -> P3.MENU_3.imageUrl
                        4 -> P3.MENU_4.imageUrl
                        5 -> ic_sec_1
                        6 -> ic_sec_2
                        7 -> ic_sec_3
                        8 -> ic_sec_4
                        9 -> ic_sec_5
                        0 -> ic_sec_6
                        else -> ic_sec_1
                    }
                )
                DogInformation(dog)
                Spacer(Modifier.weight(1f))
                DogItemButton(
                    expanded = expanded,
                    onClick = { expanded = !expanded },
                )
            }
            if (expanded) {
                DogHobby(dog)
            }
        }
    }
}

@Composable
fun DogHobby(snack: Snack) {
    Text(
        text = snack.tagline,
        modifier = Modifier.padding(horizontal = 10.dp, vertical = 8.dp),
        style = TextStyle.Default.copy(fontSize = 16.sp, fontFamily = FontFamily.SansSerif)
    )
}


@Composable
fun DogIcon(imageUrl: String, modifier: Modifier = Modifier) {
    SnackImage(
        imageUrl = imageUrl,
        contentDescription = null,
        modifier = modifier
            .size(64.dp)
            .padding(10.dp)
            .clip(MaterialTheme.shapes.small),
    )
}

@Composable
fun DogInformation(snack: Snack) {
    Column(modifier = Modifier.padding(top = 10.dp)) {
        Text(
            text = snack.name,
            modifier = Modifier,
            style = TextStyle.Default.copy(
                color = MainThemeColor,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold
            )
        )
        Text(
            text = snack.tagline.take(12).trimIndent(),
            maxLines = 1,
            style = TextStyle.Default.copy(
                color = MainThemeColor.copy(alpha = 0.5f),
                fontSize = 12.sp,
                fontWeight = FontWeight.Normal
            )
        )
    }
}

@Composable
private fun DogItemButton(
    expanded: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    IconButton(
        onClick = onClick,
        modifier = modifier
    ) {
        Icon(
            imageVector = if (expanded) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore,
            contentDescription = null,
        )
    }
}

@Preview
@Composable
fun StoryListPagePreview() {
    StoryListPage()
}